package com.crm.controller;

import com.alibaba.fastjson.JSON;
import com.crm.controller.util.R;
import com.crm.pojo.client;
import com.crm.pojo.pageBean;
import com.crm.service.clientService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/client")
public class clientController {
    @Autowired
    private clientService service;

   @GetMapping("/{uid}/{currentPage}/{pageSize}")
    public R selectAllByUid(@PathVariable Integer uid,@PathVariable Integer currentPage,@PathVariable Integer pageSize){
       pageBean<client> pageBean = service.selectByPage(uid, currentPage, pageSize);
       return new R(true,pageBean,null);
    }
    @GetMapping("/{cid}")
    public R selectByCid(@PathVariable Integer cid){
        client client = service.selectByCid(cid);
        return new R(true,client,null);
    }
    @PostMapping
    public R save(@RequestBody client c){
        c.setUid(1);
        boolean flag = service.add(c);
        return new R(flag,null,flag? "添加成功":"添加失败");
    }
    @PutMapping
    public R updateByCid(@RequestBody client c){
        boolean flag = service.updateByCid(c);//boolean flag =true;
        return new R(flag,null,flag? "修改成功":"修改失败");
    }
    @DeleteMapping("/{cid}")
    public R removeByCid(@PathVariable Integer cid){
        boolean flag = service.deleteByCid(cid);//boolean flag =true;
        return new R(flag,null,flag? "删除成功":"删除失败");
    }
    @RequestMapping("/deleteByIds")
    public R removeByCids(HttpServletRequest req) throws IOException {
        BufferedReader br = req.getReader();
        String params = br.readLine();
        int[] ids = JSON.parseObject(params, int[].class);
        int flag = service.deleteByIds(ids);
        return new R(true,null,true? "删除成功":"删除失败");
    }
}
